Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

188
Visualizações
Should setting window.location.href cause the "old" page to stop running JavaScript?

I'm seeing unexpected behavior when setting window.location.href. My understanding is that the current page will be navigated away from immediately -- effectively ignoring subsequent JavaScript in the containing script. However, this is not what I'm seeing in practice (Firefox, Chrome and mobile Safari). I'm setting window.location.href when I encounter an error condition (e.g. missing some data) and yet the script continues to run and spew a bunch of errors because of said error condition. (This also applies to window.location.assign.)

Example:

function handleError() {
    window.location.href = "https://example.com"
}

function doWork(id) {
    if (!id) {
        handleError();
    }
    var oops = id.split("-");
    // a bunch of errors spill into the console, onerror listeners, etc.
}
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

Why not simply add a return after the guard clause?

function handleError() {
    window.location.href = "https://example.com"
}

function doWork(id) {
    if (!id) {
        handleError();
        return 
    }
    var oops = id.split("-");
    // a bunch of errors spill into the console, onerror listeners, etc.
}
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda